package ModelLayer;		//makes this class a part of the ModelLayer package
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Write a description of class PersonContainer here.
 * 
 * @author (ucn-1semprojekt-dm78-gruppe1)
 * @version (a version number or a date)
 */
public class PersonContainer
{
    private ArrayList<Customer> customers;
    private ArrayList<Employee> employees;
    private ArrayList<Supplier> suppliers;
    private static PersonContainer instance;
    
    /**
     * Constructor for objects of class PersonContainer
     */
    private PersonContainer()
    {
        customers = new ArrayList<Customer>();
        employees = new ArrayList<Employee>();
        suppliers = new ArrayList<Supplier>();
    }
    
    // Singleton
    public static PersonContainer getInstance()
    {
    	if(instance == null)
    	{
    		instance = new PersonContainer();
    	}
    	return instance;
    }
    
    public void addCustomer(Customer customer)
    {
        customers.add(customer);
    }
    
    public void addEmployee(Employee employee)
    {
    	employees.add(employee);
    }
    
    public void addSupplier(Supplier supplier)
    {
    	suppliers.add(supplier);
    }
    
    public void saveToFile() throws IOException
    {
    	FileHandler.gem(customers,"Customer.db");
    	FileHandler.gem(employees,"Employee.db");
    	FileHandler.gem(suppliers,"Supplier.db");
    }
    
    @SuppressWarnings("unchecked")
	public void loadFromFile() throws IOException, ClassNotFoundException
    {
    	try {
        	customers = (ArrayList<Customer>) FileHandler.hent("Customer.db");
        	employees = (ArrayList<Employee>) FileHandler.hent("Employee.db");
        	suppliers = (ArrayList<Supplier>) FileHandler.hent("Supplier.db");
        } catch (Exception ex)
        {
        	//throw new IOException("Ikke muligt at lave et gyldigt filnavn til");
        }
    }
    
    public Customer getCustomer(String phone)
    {
        int index = 0;
        int index2 = 0;
        boolean found = false;
        while (!found && index < customers.size())
        {
        	Customer mycustomer = customers.get(index);
            if(mycustomer.getPhone().equals(phone))
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return customers.get(index2);
        }
        else
        {
            return null;
        }
    }
    
    public Employee getEmployee(String phone)
    {
        int index = 0;
        int index2 = 0;
        boolean found = false;
        while (!found && index < employees.size())
        {
        	Employee myemployees = employees.get(index);
            if(myemployees.getPhone().equals(phone))
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return employees.get(index2);
        }
        else
        {
            return null;
        }
    }
    
    public Supplier getSupplier(String phone)
    {
        int index = 0;
        int index2 = 0;
        boolean found = false;
        while (!found && index < suppliers.size())
        {
        	Supplier mysupplier = suppliers.get(index);
            if(mysupplier.getPhone().equals(phone))
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return suppliers.get(index2);
        }
        else
        {
            return null;
        }
    }
    
    public List<Supplier> getSupplierList()
    {
    	return this.suppliers;
    }
    
    public List<Customer> getCustomerList()
    {
    	return this.customers;
    }
    
    public List<Employee> getEmployeeList()
    {
    	return this.employees;
    }
    
    public Employee getFirstEmployee()
    {
    	if (employees.isEmpty())
    	{
    		return null;
    	}
    	else
    	{
    		return employees.get(0);
    	}
    }
}
